package cn.com.ctcloud.microservice.dynamicds.database.druid.dashboard;

import org.springframework.aop.support.DefaultPointcutAdvisor;
import org.springframework.aop.support.JdkRegexpMethodPointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;

import com.alibaba.druid.support.spring.stat.DruidStatInterceptor;
import com.alibaba.druid.util.StringUtils;

import cn.com.ctcloud.microservice.dynamicds.database.druid.DruidDBConfig;

/**
 * Title: 核心类库
 * <p>
 * Description: druid进行spring监控
 * <p>
 * Copyright: Copyright (c) 2008
 * <p>
 * Company: Liaoning Open Data Communication Co.,Ltd.
 *
 * @author lihui
 * @version 1.0.0
 */
@Configuration
public class SpringDaoMethodAspect {
	@Autowired(required = false)
	private DruidDBConfig druidDBConfig;

	@Bean
	public DruidStatInterceptor druidStatInterceptor() {
		final DruidStatInterceptor dsInterceptor = new DruidStatInterceptor();
		return dsInterceptor;
	}

	@Bean
	@Scope("prototype")
	public JdkRegexpMethodPointcut druidStatPointcut() {
		final JdkRegexpMethodPointcut pointcut = new JdkRegexpMethodPointcut();
		if (StringUtils.isEmpty(druidDBConfig.getTypeAliasesPackage())) {
			pointcut.setPattern("cn.com.ctcloud.microservice.*.dao.*");
		} else {
			pointcut.setPattern(druidDBConfig.getTypeAliasesPackage());
		}
		return pointcut;
	}

	@Bean
	public DefaultPointcutAdvisor druidStatAdvisor(final DruidStatInterceptor druidStatInterceptor,
			final JdkRegexpMethodPointcut druidStatPointcut) {
		final DefaultPointcutAdvisor defaultPointAdvisor = new DefaultPointcutAdvisor();
		defaultPointAdvisor.setPointcut(druidStatPointcut);
		defaultPointAdvisor.setAdvice(druidStatInterceptor);
		return defaultPointAdvisor;
	}
}